//
//  GTDeleteCellView.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/12.
//

#import "GTDeleteCellView.h"

@interface GTDeleteCellView ()

@property (nonatomic, strong, readwrite) UIView *backgroundView;
@property (nonatomic, strong, readwrite) UIButton *deleteButton;
@property (nonatomic, copy, readwrite) dispatch_block_t deleteBlock;
@end

@implementation GTDeleteCellView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        [self addSubview:({
            self.backgroundView = [[UIView alloc]initWithFrame:self.bounds];
            self.backgroundView.backgroundColor = [UIColor blackColor];
            self.backgroundView.alpha = 0.5;

            [self.backgroundView addGestureRecognizer:({
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                                            action:@selector(dismissDeleteView)];
                tapGesture;
            })];
            self.backgroundView;
        })];
        [self addSubview:({
            self.deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
            [self.deleteButton addTarget:self
                                  action:@selector(_clickButton)
                        forControlEvents:UIControlEventTouchUpInside];
            self.deleteButton.backgroundColor = [UIColor blueColor];
            self.deleteButton;
        })];
    }

    return self;
}

- (void)showDeleteViewFromPoint:(CGPoint)point clickBlock:(dispatch_block_t)clickBlock {
//	[[UIApplication sharedApplication].keyWindow addSubview:self];//被废弃ios13
    self.deleteButton.frame = CGRectMake(point.x, point.y, 0, 0);
    self.deleteBlock = [clickBlock copy];
    [UIApplication.sharedApplication.windows.lastObject addSubview:self];    //被废弃ios15

//	[UIView animateWithDuration:1.f animations:^{
//		self.deleteButton.frame = CGRectMake((self.bounds.size.width-200)/2, (self.bounds.size.height-200)/2, 200, 200);
//	}];

    [UIView animateWithDuration:1.f
                          delay:0.f
         usingSpringWithDamping:0.5
          initialSpringVelocity:0.5
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
        self.deleteButton.frame = CGRectMake((self.bounds.size.width - 200) / 2, (self.bounds.size.height - 200) / 2, 200, 200);
    }
	completion:^(BOOL finished) {
        NSLog(@"");
    }];
	
//	CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"tranform.scale"];
//	scaleAnimation.keyTimes= @[@0,@1];
//	scaleAnimation.values = @[@0, @1];
//	scaleAnimation.duration = 0.4;
	
	CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
	opacityAnimation.keyTimes = @[@0,@1];
	opacityAnimation.values = @[@0, @1];
	opacityAnimation.duration = 1;
//	opacityAnimation.beginTime = 0.2;
	
	CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
	animationGroup.duration = 1;
	animationGroup.removedOnCompletion = NO;
	animationGroup.fillMode = kCAFillModeForwards;
	animationGroup.animations = @[opacityAnimation];
	
	[self.layer addAnimation:animationGroup forKey:@"animationGroup"];
}

- (void)dismissDeleteView {
    [self removeFromSuperview];
}

- (void)_clickButton {
    if (self.deleteBlock) {
        self.deleteBlock();
    }

    [self removeFromSuperview];
}

@end
